package cn.bossfriday.im.access.actors;

import cn.bossfriday.common.register.ActorRoute;
import cn.bossfriday.common.rpc.actor.ActorRef;
import cn.bossfriday.im.common.conf.ConfigurationAllLoader;
import cn.bossfriday.im.common.entity.result.Result;
import cn.bossfriday.im.common.message.rpc.access.GetAccessAddressInput;
import cn.bossfriday.im.common.message.rpc.access.GetAccessAddressOutput;
import cn.bossfriday.im.common.rpc.BaseActor;
import lombok.extern.slf4j.Slf4j;

import static cn.bossfriday.im.common.constant.ImConstant.ACTOR_ACCESS_GET_ACCESS_ADDRESS;
import static cn.bossfriday.im.common.entity.result.ResultCode.SYSTEM_ERROR;

/**
 * GetAccessAddressActor
 *
 * @author chenx
 */
@Slf4j
@ActorRoute(methods = ACTOR_ACCESS_GET_ACCESS_ADDRESS)
public class GetAccessAddressActor extends BaseActor<GetAccessAddressInput> {

    @Override
    public void onMessageReceived(GetAccessAddressInput msg) {
        try {
            String publicIp = ConfigurationAllLoader.getInstance().getImAccessConfig().getPublicIp();
            int port = ConfigurationAllLoader.getInstance().getImAccessConfig().getMqttPort();
            String accessAddress = publicIp + ":" + port;
            GetAccessAddressOutput output = GetAccessAddressOutput.builder()
                    .accessAddress(accessAddress)
                    .build();
            this.getSender().tell(output, ActorRef.noSender());
        } catch (Exception ex) {
            log.error("GetAccessAddressActor.onMessageReceived() error!", ex);
            this.getSender().tell(Result.error(SYSTEM_ERROR), ActorRef.noSender());
        }
    }
}
